<?php
/**
 * User: 钟贵廷
 * Date: 2017/5/18
 * Time: 20:18
 * 微信群发功能
 *
 */
namespace app\admin\controller;

class MassSend extends Admin
{

    //1、上传图文消息内的图片获取URL【订阅号与服务号认证后均可用】
    //这个接口要注意的地方是使用curl_file_create($image)发送图片
    /**
     * 返回结果
     *
     * Array
     * (
     * [url] => http://mmbiz.qpic.cn/mmbiz_png/y4nxYMK2vc3lQlF5CZXGgCumauF0K4kklOhrsic9t7iaBt2nbiaG0ibXhF5WXeDibuvsm5fomHFHlpLpK65kWycqfCg/0
     * )
     */
    public function getUrl()
    {
        $access_token = $this->get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}";
        $image = '/home/wwwroot/tp5_tomrepair/public/assets/admin/images/login.png';
        $fileData = ['media' => curl_file_create($image)];
        $res = $this->https_request($url, $fileData);
        p($res);
    }

    //2、上传图文消息素材【订阅号与服务号认证后均可用】
    //这个接口要注意啦，thumb_media_id必须是要通过多媒体上传接口上传的文件
    //Array
    //(
    //[type] => news
    //[media_id] => Ox02LlDsXk4bgMXT0ER-i3IVrPeck-Z-rHrwz8FzUQwylyUgoCS1HTN3uIZawS5K
    //[created_at] => 1495157292
    //)
    public function uploadNews()
    {
        $access_token = $this->get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={$access_token}";
        $model = new \app\admin\model\MassSend();
        $data = $model->uploadNewsData();
        p($data);
        $res = $this->https_request($url, $this->cn_json_encode($data));
        p($res);
    }

    /**
     * access_token 调用接口凭证
     * type 媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
     * media form-data中媒体文件标识，有filename、filelength、content-type等信息
     * 参考文档：http://www.cnblogs.com/txw1958/p/weixin80-upload-download-media-file.html
     */
    public function uploadMedia()
    {
        $access_token = $this->get_access_token();
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
        $image = '/home/wwwroot/tp5_tomrepair/public/assets/admin/images/login.png';
        $fileData = ['media' => curl_file_create($image)];
        $res = $this->https_request($url, $fileData);
        p($res);
    }


    //4、根据OpenID列表群发【订阅号不可用，服务号认证后可用】
    public function send()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={$this->get_access_token()}";
        $model = new \app\admin\model\MassSend();
        $data = $this->cn_json_encode($model->sendMpNewsData());
        p($model->sendMpNewsData());
        p($data);
        $res = $this->https_request($url, $data);
        p($res);
    }


    public function test()
    {
        $str = '{
       "touser":[
        "OPENID1",
        "OPENID2"
       ],
       "mpnews":{"media_id":"123dsdajkasd231jhksad"},
        "msgtype":"mpnews",
        "send_ignore_reprint":0
    }';
        p(json_decode($str,true));
    }

    public function test2()
    {
        echo $this->get_access_token();
    }

}